package com.myapp.ruyixing.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Member;
import com.myapp.ruyixing.model.Member.MemberItem;
import com.myapp.ruyixing.model.Message;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.CompatibilityUtils;
import com.myapp.ruyixing.utils.LoginHelper;

public class MemberFragment extends Fragment implements OnClickListener {

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			mTitleBar.showLoading(false);

			if (getActivity() != null && isAdded() && !isDetached()
					&& !isRemoving() && isVisible()) {
				Member member = (Member) o;
				renderData(member);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			mTitleBar.showLoading(false);

			if (getActivity() != null) {
				Toast.makeText(getActivity(), throwable.getMessage(),
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	private RYHttpResponseHandler mApplyHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			mTitleBar.showLoading(false);

			Message message = (Message) o;
			if (message != null) {
				if (message.errno == 0) {
					if (getActivity() != null && isAdded() && !isDetached()
							&& !isRemoving() && isVisible()) {
						startActivity(new Intent(getActivity(),
								GetMemberCardActivity.class));
					}
				} else if (getActivity() != null) {
					Toast.makeText(getActivity(), message.msg,
							Toast.LENGTH_SHORT).show();
				}
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			mTitleBar.showLoading(false);

			if (getActivity() != null) {
				Toast.makeText(getActivity(), throwable.getMessage(),
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	private TitleBar mTitleBar;
	private ViewGroup mCardGroup;

	private RequestQueue mQueue;
	private ImageLoader mImageLoader;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_member, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		mTitleBar = (TitleBar) view.findViewById(R.id.title_bar);
		mCardGroup = (LinearLayout) view.findViewById(R.id.cards);

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		mQueue = Volley.newRequestQueue(getActivity());
		mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		RYHttpClient.getMemberDetail(getActivity(), mHandler);
		mTitleBar.showLoading(true);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		default:
			break;
		}
	}

	public void setTitleImageIcon(int iconId, OnClickListener listener) {
		mTitleBar.setIconDrawable(iconId);
		mTitleBar.setOnIconClickedListener(listener);
	}

	private void renderData(Member member) {
		if (member.items != null && member.items.size() > 0) {
			for (MemberItem item : member.items) {
				View cell = LayoutInflater.from(getActivity()).inflate(
						R.layout.cell_member_fragment, mCardGroup, false);
				renderCell(cell, item);

				mCardGroup.addView(cell);
			}
		}
	}

	private void renderCell(View view, final MemberItem item) {
		final View mGetCardView = view.findViewById(R.id.get_card);
		mGetCardView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!LoginHelper.getInstance().ensureLogin(getActivity())) {
					return;
				}
				
				RYHttpClient.applyMemberCard(getActivity(), mApplyHandler,
						item.cardid);
			}
		});

		final View mDescExpandActionView = view.findViewById(R.id.card_expand);
		final TextView mCardDescView = (TextView) view
				.findViewById(R.id.card_desc);
		mCardDescView.setText(item.content);
		mCardDescView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mCardDescView.setMaxLines(Integer.MAX_VALUE);
				mDescExpandActionView.setVisibility(View.GONE);
			}
		});

		final NetworkImageView picView = (NetworkImageView) view
				.findViewById(R.id.card_pic);
		picView.setImageUrl(item.picurl, mImageLoader);

		TextView mTitelView = (TextView) view.findViewById(R.id.card_title);
		mTitelView.setText(item.title);

		ViewTreeObserver vto = mCardDescView.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				ViewTreeObserver observer = mCardDescView.getViewTreeObserver();
				CompatibilityUtils.removeOnGlobalLayoutListener(observer, this);

				if (mCardDescView.getLineCount() > 2) {
					mDescExpandActionView.setVisibility(View.VISIBLE);
					mCardDescView.setMaxLines(2);
				} else {
					mDescExpandActionView.setVisibility(View.GONE);
				}
			}
		});
	}

}
